package cn.hiapi.shopkeeper.service;

import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shop.service.product.ProductNumService;
import cn.hiapi.shopkeeper.consts.AppConst;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.service.order.ActivityBargainOrderService;
import cn.hiapi.shopkeeper.service.order.ActivityBargainRecordService;
import cn.hiapi.shopkeeper.service.order.ActivityGroupOrderService;
import cn.hiapi.shopkeeper.service.order.ActivityLotteryOrderService;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Service;

/**
 * @author AdinZ
 * @date 2023/2/28 18:32
 * 活动销量和参与人数
 */
@Service
public class ActivityNumber {

    private final DispatchContext dispatchContext;


    public ActivityNumber(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }


    public Total get(ActivityType type,long id){

        Total total=new Total();
        long sales = this.dispatchContext.getServiceOne(ProductNumService.class).totalByGid(id, AppConst.ACTIVITY_SELL_TOTAL);
        total.setSales(sales);
        total.setParticipate(sales);
        if (type==ActivityType.BARGAIN){
            total.setParticipate(this.dispatchContext.getServiceOne(ActivityBargainOrderService.class).getParticipate(id));
        }else if(type==ActivityType.GROUP){
            total.setParticipate(this.dispatchContext.getServiceOne(ActivityGroupOrderService.class).getParticipate(id));
        }else if(type==ActivityType.LOTTERY){
            total.setParticipate(this.dispatchContext.getServiceOne(ActivityLotteryOrderService.class).count(new QueryWrapper().eq("aid",id)));
        }
        return total;
    }

    @Setter
    @Getter
    public class Total{
        @ApiModelProperty("销量")
        private long sales;

        @ApiModelProperty("参与人数")
        private long participate;
    }

}
